
/* For detailed instructions, see README file */

OPTIONS SOURCE MACROGEN SYMBOLGEN MPRINT COMPRESS=YES REUSE=YES SORTSIZE= MAX;



/*=========================================*/
/*Select path where files are saved*/
libname OP "INSERT PATH";
/*Select path to save logs*/
%let LOG_PATH=INSERT PATH\Figures.log;
options dlcreatedir; 
%PUT &LOG_PATH;
/*=========================================*/


proc printto log="&LOG_PATH" new;
run;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS NORESULTS;
ODS SELECT NONE;
/*=========================================*/


data OPTION_DATA;
SET OP.OPRA_Pseudo;
	

TOTAL_COUNT=1;

LOB_IND= 1- AUCTION_IND;
N_PFOF_IND = 1- PFOF_IND;

PFOF_LOB = 			PFOF_IND*	LOB_IND;
PFOF_AUC = 			PFOF_IND*	AUCTION_IND;

NPFOF_LOB = 		N_PFOF_IND*	LOB_IND;
NPFOF_AUC = 		N_PFOF_IND*	AUCTION_IND;


IF PFOF_IND=1 THEN LABEL_PFOF=PUT("PFOF DMM",$12.); ELSE LABEL_PFOF=PUT("Non-PFOF DMM",$24.);
IF AUCTION_IND=1 THEN LABEL_AUCTION=PUT("Auctions",$12.); ELSE LABEL_AUCTION=PUT("Continuous",$24.);

KEEP


QUOTEDSPREAD_C
EFFECTIVESPREAD_C
MMREV_C
PIMP_C
PFOF_IND
LABEL_PFOF
LABEL_AUCTION

;



RUN;





proc tabulate data= OPTION_DATA format=8.2 OUT=MEANS(
RENAME= (

EFFECTIVESPREAD_C_MEAN=EFFECTIVESPREAD_C
MMREV_C_MEAN=MMREV_C
PIMP_C_MEAN=PIMP_C



));

;
VAR 
EFFECTIVESPREAD_C
MMREV_C
PIMP_C


;
class LABEL_PFOF LABEL_AUCTION;
TABLES

	 LABEL_AUCTION*EFFECTIVESPREAD_C*MEAN
	 LABEL_AUCTION*MMREV_C*MEAN
	 LABEL_AUCTION*PIMP_C*MEAN


;

 RUN;

 DATA MEANS;
 	SET MEANS;
	DROP _TYPE_ _PAGE_ _TABLE_;
RUN;

PROC SORT DATA=MEANS; BY LABEL_AUCTION;RUN;
PROC TRANSPOSE DATA=MEANS OUT=MEANS_T; BY LABEL_AUCTION;RUN;

 DATA MEANS_T;
 	SET MEANS_T;
	IF _NAME_ ="EFFECTIVESPREAD_C" THEN LABEL= 1 ;
	IF _NAME_ ="MMREV_C" THEN LABEL= 2 ;
	IF _NAME_ ="PIMP_C" THEN LABEL= 0;
	IF LABEL_AUCTION="Auctions" THEN TYPE_2=0; ELSE TYPE_2=1;
	RENAME COL1=MEANS;
	RUN;

proc format;
    value label_fmt
        0 = "Auctions"
        1 = "Continuous";
run;
proc format;
    value label_fmtT
        2 = "MM Rev."
        1 = "Effective Spread"
		0= "Price Improvement";
run;



ODS GRAPHICS OFF;
ODS RESULTS;
ODS SELECT ALL;
ODS TITLE "FIGURE 1 Panel A";
ODS GRAPHICS / ATTRPRIORITY=NONE;
proc sgplot data=MEANS_T;
    format TYPE_2 label_fmt.; 
    format LABEL label_fmtT.; 
    styleattrs datacolors=(green blue red);
    vbar TYPE_2 / response=MEANS group=LABEL groupdisplay=stack;
    xaxis label=" " labelattrs=(size=12 weight=bold) discreteorder=data;
    yaxis label="Mean" labelattrs=(size=12 weight=bold);
    keylegend / title="Legend" location=outside position=right;
run;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS NORESULTS;
ODS SELECT NONE;


data OPTION_DATA;
SET OP.OPRA_Pseudo;
	

TOTAL_COUNT=1;

LOB_IND= 1- AUCTION_IND;
N_PFOF_IND = 1- PFOF_IND;

PFOF_LOB = 			PFOF_IND*	LOB_IND;
PFOF_AUC = 			PFOF_IND*	AUCTION_IND;

NPFOF_LOB = 		N_PFOF_IND*	LOB_IND;
NPFOF_AUC = 		N_PFOF_IND*	AUCTION_IND;


IF PFOF_IND=1 THEN LABEL_PFOF=PUT("PFOF DMM",$12.); ELSE LABEL_PFOF=PUT("Non-PFOF DMM",$24.);
IF AUCTION_IND=1 THEN LABEL_AUCTION=PUT("Auctions",$12.); ELSE LABEL_AUCTION=PUT("Continuous",$24.);

KEEP


QUOTEDSPREAD_C
EFFECTIVESPREAD_C
MMREV_C
PIMP_C
PFOF_IND
LABEL_PFOF
LABEL_AUCTION

;



RUN;




proc tabulate data= OPTION_DATA format=8.2 OUT=MEANS(
RENAME= (

EFFECTIVESPREAD_C_MEAN=EFFECTIVESPREAD_C
MMREV_C_MEAN=MMREV_C
PIMP_C_MEAN=PIMP_C



));

;
VAR 
EFFECTIVESPREAD_C
MMREV_C
PIMP_C


;
class LABEL_PFOF LABEL_AUCTION;
TABLES



	 LABEL_AUCTION*LABEL_PFOF*EFFECTIVESPREAD_C*MEAN
	 LABEL_AUCTION*LABEL_PFOF*MMREV_C*MEAN
	 LABEL_AUCTION*LABEL_PFOF*PIMP_C*MEAN

;

 RUN;

 DATA MEANS;
 	SET MEANS;
	DROP _TYPE_ _PAGE_ _TABLE_;

	IF LABEL_PFOF="PFOF DMM";
RUN;

PROC SORT DATA=MEANS; BY LABEL_AUCTION;RUN;
PROC TRANSPOSE DATA=MEANS OUT=MEANS_T; BY LABEL_AUCTION; RUN;

 DATA MEANS_T;
 	SET MEANS_T;
	IF _NAME_ ="EFFECTIVESPREAD_C" THEN LABEL= 1 ;
	IF _NAME_ ="MMREV_C" THEN LABEL= 2 ;
	IF _NAME_ ="PIMP_C" THEN LABEL= 0;
	IF LABEL_AUCTION="Auctions" THEN TYPE_2=0; ELSE TYPE_2=1;
	RENAME COL1=MEANS;
	RUN;

/* Define the custom format */
proc format;
    value label_fmt
        0 = "Auctions"
        1 = "Continuous";
run;
proc format;
    value label_fmtT
        2 = "MM Rev."
        1 = "Effective Spread"
		0= "Price Improvement";
run;


ODS GRAPHICS OFF;
ODS RESULTS;
ODS SELECT ALL;
ODS TITLE "FIGURE 1: Components of Spread - Panel B";
FOOTNOTE "This figure illustrates components of spread measured using pseudo OPRA data.";
/* Create the stacked bar plot */
proc sgplot data=MEANS_T;
    format TYPE_2 label_fmt.; 
	format LABEL label_fmtT.; 
    vbar TYPE_2 / response=MEANS group=LABEL groupdisplay=stack;
    xaxis label=" " labelattrs=(size=12 weight=bold);
    yaxis label="Mean" labelattrs=(size=12 weight=bold);
    keylegend / title="Legend" location=outside position=right;
run;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS RESULTS OFF;
ODS SELECT NONE;














data OPTION_DATA;
SET OP.OPRA_Pseudo;
	

TOTAL_COUNT=1;

LOB_IND= 1- AUCTION_IND;
N_PFOF_IND = 1- PFOF_IND;

PFOF_LOB = 			PFOF_IND*	LOB_IND;
PFOF_AUC = 			PFOF_IND*	AUCTION_IND;

NPFOF_LOB = 		N_PFOF_IND*	LOB_IND;
NPFOF_AUC = 		N_PFOF_IND*	AUCTION_IND;


IF PFOF_IND=1 THEN LABEL_PFOF=PUT("PFOF DMM",$12.); ELSE LABEL_PFOF=PUT("Non-PFOF DMM",$24.);
IF AUCTION_IND=1 THEN LABEL_AUCTION=PUT("Auctions",$12.); ELSE LABEL_AUCTION=PUT("Continuous",$24.);

KEEP


LABEL_PFOF
LABEL_AUCTION
TOTAL_COUNT

D_VOL
AUCTION_IND
PFOF_IND

;



RUN;





proc tabulate data= OPTION_DATA format=8.2 OUT=PCTSUM_TOTAL(RENAME= (D_VOL_PctSum_00_D_VOL = PCTSUM));

;
VAR TOTAL_COUNT D_VOL;
class LABEL_PFOF LABEL_AUCTION;
TABLES

	  LABEL_AUCTION*LABEL_PFOF*D_VOL*PCTSUM
	 LABEL_PFOF*D_VOL*PCTSUM
;

 RUN;

 DATA PCTSUM_TOTAL;
 	SET PCTSUM_TOTAL;
	IF MISSING(LABEL_AUCTION)=1 THEN LABEL_AUCTION="All";
	DROP _TYPE_ _PAGE_ _TABLE_;
RUN;


 DATA PCTSUM_TOTAL;
 	SET PCTSUM_TOTAL;
	IF LABEL_PFOF="PFOF DMM" THEN TYPE_2=0; ELSE TYPE_2=1;
	RUN;
proc format;
		value typefmt
		0 = "PFOF"
		1 = "NPFOF";
run;



ODS GRAPHICS OFF;
ODS RESULTS;
ODS SELECT ALL;
ODS TITLE "FIGURE 2: DMM Volumes (Pseudo Data)";
FOOTNOTE "This figure plots traded volume for designated market makers (DMMs), comparing those
 that pay significant payment for order flow (PFOF) with Non-PFOF DMMs using pseudo
 OPRA data.";

proc sgplot data=PCTSUM_TOTAL;
	format TYPE_2 typefmt.; 
    vbar LABEL_AUCTION / response=PCTSUM group=TYPE_2 groupdisplay=cluster fillattrs=(color=lightgray);
    xaxis label=" " labelattrs=(size=12 weight=bold);
    yaxis label="Volume(%)" labelattrs=(size=12 weight=bold);
    keylegend / title=" " location=outside position=right;
RUN;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS NORESULTS;
ODS SELECT NONE;




















DATA TABLE_DATA_AUCTIONS;
SET OP.OPRA_Pseudo;


	IF AUCTION_IND=1;




RUN;





/*BAR PLOT*/
DATA BAR_DATA;
	SET TABLE_DATA_AUCTIONS;


		count=1;

		IF Multiple_IND=1 THEN Multiple=put("Yes",$3.); else Multiple=put("No",$3.);
		IF  Multiple="Yes" THEN MULTIPLE_IND=1; ELSE MULTIPLE_IND=0;

		IF round(input(PImp_C,best12.),1)=0 or round(input(PImp_C,best12.),1)=1 then x_axis_data_3 = put(put(round(PImp_C,1),$2.),$64.);
		ELSE IF 1 < round(input(PImp_C,best12.),1) < 10 THEN x_axis_data_3 = put("2 to 9",$64.);
		ELSE x_axis_data_3 = put("> 9",$64.);

		IF round(input(PImp_C,best12.),1)<=10 then x_axis_data_2 = put(put(round(PImp_C,1),$2.),$64.);
		ELSE x_axis_data_2=put(">10",$64.);

		IF PFOF_IND=1 THEN TYPE=PUT("PFOF DMM",$12.); ELSE TYPE=PUT("Non-PFOF DMM",$12.);



		keep  Multiple PImp_C count DMM_IDENTITY  x_axis_data_3 x_axis_data_2 TYPE;
run;


/*=========================================================*/
PROC SORT DATA=BAR_DATA; BY TYPE;RUN;
proc freq data=BAR_DATA order=internal ;
   tables x_axis_data_3 / NOFREQ  nocol norow nocum  out=freqcnt_3 ;
   BY TYPE;
run;
PROC SORT DATA=BAR_DATA; BY TYPE;RUN;

proc freq data=BAR_DATA order=internal ;
   tables x_axis_data_2 / NOFREQ  nocol norow nocum  out=TEMP;
   BY TYPE;
   RUN;
DATA TEMP;	
	SET TEMP;
	x_axis_data_2 = compress(x_axis_data_2);
	IF x_axis_data_2 in ("0","1","10",">10") then delete;
	x_axis_data_3 =  put("2 to 9",$64.);
run;
proc sql;
	create table TEMP_2
	AS SELECT
	MEAN(PERCENT) AS PERCENT,
	x_axis_data_3, TYPE
	FROM TEMP
	GROUP BY TYPE, x_axis_data_3;
QUIT;
PROC SQL;
	CREATE TABLE freqcnt_4
	AS SELECT
	A.*, B.PERCENT AS PERCENT_AVERAGE
	FROM freqcnt_3 A LEFT JOIN TEMP_2 B
	ON A.x_axis_data_3= B.x_axis_data_3 & A.TYPE = B.TYPE;
QUIT;


DATA freqcnt_5;
	SET freqcnt_4;
	IF MISSING(PERCENT_AVERAGE)=0 THEN PERCENT= PERCENT_AVERAGE;
	IF x_axis_data_3= "0" THEN POSITION=1;
	ELSE IF  x_axis_data_3= "1" THEN POSITION=2;
	ELSE IF  x_axis_data_3=  "2 to 9" THEN POSITION=3;
	ELSE IF  x_axis_data_3=  "> 9" THEN POSITION=4;
	IF TYPE="PFOF DMM" THEN TYPE_2=0; ELSE TYPE_2=1;
RUN;

PROC SORT DATA=freqcnt_5; 
    BY POSITION  
       TYPE_2; 
RUN;
proc format;
		value typefmt
		0 = "PFOF"
		1 = "NPFOF";
run;




ODS GRAPHICS OFF;
ODS RESULTS;
ODS SELECT ALL;
ODS TITLE "FIGURE 3: Auction Frequency versus Price Improvement (Pseudo Data)";
FOOTNOTE "This figure plots auction frequency against price improvement using pseudo OPRA data.";
proc sgplot data=freqcnt_5;
	format TYPE_2 typefmt.;
    vbar x_axis_data_3 / response=PERCENT group=TYPE_2 groupdisplay=cluster fillattrs=(color=lightgray);
    xaxis label="Price Improvement (cents)" labelattrs=(size=12 weight=bold);
    yaxis label="Frequency (%)" labelattrs=(size=12 weight=bold);
    keylegend / title=" " location=outside position=right;
RUN;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS NORESULTS;
ODS SELECT NONE;



PROC SORT DATA=BAR_DATA; BY x_axis_data_2;RUN;
proc freq data=BAR_DATA order=internal ;
   tables Multiple*TYPE / NOFREQ  nocol norow nocum  out=freqcnt_2 ;
   BY x_axis_data_3;
run;

DATA freqcnt_2;
	SET freqcnt_2;
	IF MULTIPLE="Yes";
RUN;
proc freq data=BAR_DATA order=internal ;
   tables Multiple*TYPE / NOFREQ  nocol norow nocum  out=TEMP;
   BY x_axis_data_2;
run;
DATA TEMP;
	SET TEMP;
	IF MULTIPLE="Yes";
RUN;
DATA TEMP;	
	SET TEMP;
	x_axis_data_2 = compress(x_axis_data_2);
	IF x_axis_data_2 in ("0","1","10",">10") then delete;
	x_axis_data_3 =  put("2 to 9",$64.);
run;
proc sql;
	create table TEMP_2
	AS SELECT
	MEAN(PERCENT) AS PERCENT,
	x_axis_data_3, TYPE
	FROM TEMP
	GROUP BY TYPE, x_axis_data_3;
QUIT;
PROC SQL;
	CREATE TABLE freqcnt_3
	AS SELECT
	A.*, B.PERCENT AS PERCENT_AVERAGE
	FROM freqcnt_2 A LEFT JOIN TEMP_2 B
	ON A.x_axis_data_3= B.x_axis_data_3 & A.TYPE = B.TYPE;
QUIT;
DATA freqcnt_3;
	SET freqcnt_3;
	IF MISSING(PERCENT_AVERAGE)=0 THEN PERCENT= PERCENT_AVERAGE;
RUN;

DATA freqcnt_3_2;
	SET freqcnt_3;
	IF MISSING(PERCENT_AVERAGE)=0 THEN PERCENT= PERCENT_AVERAGE;
	IF x_axis_data_3= "0" THEN POSITION=1;
	ELSE IF  x_axis_data_3= "1" THEN POSITION=2;
	ELSE IF  x_axis_data_3=  "2 to 9" THEN POSITION=3;
	ELSE IF  x_axis_data_3=  "> 9" THEN POSITION=4;
	IF TYPE="PFOF DMM" THEN TYPE_2=0; ELSE TYPE_2=1;
RUN;




ODS GRAPHICS OFF;
ODS RESULTS;
ODS SELECT ALL;
ODS TITLE "FIGURE 4: Auction Competition with Multiple Bidders (Pseudo Data)";
FOOTNOTE "This figure plots frequencies for auctions with multiple bidders against price improvement
 using pseudo OPRA data.";
proc sgplot data=freqcnt_3_2;
	format TYPE_2 typefmt.;
    vbar x_axis_data_3 / response=PERCENT group=TYPE_2 groupdisplay=cluster fillattrs=(color=lightgray);
    xaxis label="Price Improvement (cents)" labelattrs=(size=12 weight=bold);
    yaxis label="Frequency (%)" labelattrs=(size=12 weight=bold);
    keylegend / title=" " location=outside position=right;
RUN;
FOOTNOTE " ";
ODS TITLE " ";
ODS GRAPHICS OFF;
ODS NORESULTS;
ODS SELECT NONE;



proc printto log=log;
run;















